package com.ktjy.goods.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.common.exception.ServiceException;
import com.ktjy.common.utils.StringUtils;
import com.ktjy.common.utils.bean.BeanValidators;
import com.ktjy.goods.service.IGoodsService;
import com.ktjy.goods.domain.Goods;
import com.ktjy.goods.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;


/**
 * 规格属性Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-13
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements IGoodsService {
    @Autowired
    protected Validator validator;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询规格属性列表
     *
     * @param goods 规格属性
     * @return 规格属性
     */
    @Override
    public List<Goods> selectAttrList(Goods goods)
    {
        return goodsMapper.selectAttrList(goods);
    }


    /**
     * 导入用户数据
     *
     * @param goodsList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importUser(List<Goods> goodsList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(goodsList) || goodsList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Goods goods : goodsList)
        {
            try
            {
                // 验证是否存在这个用户
                Goods u = goodsMapper.selectOne(new QueryWrapper<Goods>().eq("name", goods.getName()));
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, goods);
                    goods.setCreateBy(operName);
                    goodsMapper.insert(goods);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 、" + goods.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, goods);
                    goods.setUpdateBy(operName);
                    goodsMapper.update(goods,new QueryWrapper<Goods>().eq("name", goods.getName()));
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、 " + goods.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、 " + goods.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、 " + goods.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}